#include "acc_user.h"
#include "veriuser.h"

// Define the ON and OFF time of clock
#define PERIOD  5 

// Data structure
struct clkData {
  int clk;
  int clkCnt;
};

// Main routine which toggles the clock
void clkGen () {
  // Get the stored workarea
  struct clkData *data = ( struct clkData * )tf_igetworkarea(tf_getinstance());
  if (data->clkCnt == PERIOD) {
    data->clk = (data->clk == 0) ? 1 : 0;
    data->clkCnt = 0;
    //io_printf("%d Current clk = %d\n",tf_gettime(), data->clk);
  } else {
    data->clkCnt ++;
  }
  // Drive the clock signal in HDL
  tf_putp (1, data->clk);
}

// checktf() routine
// This function inits the objects and also stores the object in workarea
void clkInit() {
  struct clkData *data = ( struct clkData * )malloc( sizeof( struct clkData ) );
  data->clkCnt = 0;
  data->clk    = 0;
  tf_setworkarea(data);
}

// misctf() routine
// This routine is called after 1 tick
void clkReactive (int data, int reason, int paramvc) {
  // if callback reason is reactive, then call clkGen function
  if (reason == reason_reactivate) {
    clkGen();
  }
  // Set the callback delay to 1 tick
  tf_setdelay(1);
}
